package core.modules;
/**
 * 
 */
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.util.Log;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;


/**
 * @author  GlennMark Arnobit
 *
 */
public abstract class DatabaseConnector {

	/* ------------- Configurable Database Parameters*/
	
	/** Host deployed IP or Domain */
	private static String REMOTE_HOST_NAME = "192.168.20.2";
	//10.97.0.67
	
	/** Host Storage DIR */
	private static String ROOT_DIRECTORY = "trac";
	
	/** Sub Directory for Catalog Files */
	private static String REMOTE_SUB = "";
	
	/** 
	 * Host Database Port - default MySQL port of 3306
	 * @return 3306
	 * */
	private static final Integer REMOTE_PORT = 3306;
	
	/** Host Database Name */
	/** @return {@value #REMOTE_DB_NAME}*/
	private static final String REMOTE_DB_NAME = "trac";
	
	/** Host Database Username */
	/** @return {@value #DB_USERNAME}*/
	private static final String DB_USERNAME = "TRAC";
	
	/** Host Database Password */
	/** {@value #DB_PASSWORD} */
	private static final String DB_PASSWORD = "";
	
	private static String DRIVER_NAME = "com.mysql.jdbc.Driver";
	
	private static String JDBC_URL = "jdbc:mysql://" + REMOTE_HOST_NAME + ":" + REMOTE_PORT + "/" + REMOTE_DB_NAME;
	

/* ------------------------------------------ ** DO NOT EDIT BEYOND THIS LINE ** */
	
	// ------------------------------------------
	//					HOST Methods
	// ------------------------------------------
	
	/**
	 * set the host name for the server location
	 * */
	public static void setHostName(String hostName) {

		String PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
				+ "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
				+ "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
				+ "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

		Pattern pattern = Pattern.compile(PATTERN);
		Matcher matcher = pattern.matcher(hostName);

		if (matcher.matches()) {
			DatabaseConnector.REMOTE_HOST_NAME = hostName;
		}

	}
	
	/** 
	 * The Server's location 
	 * */
	public static String getHostName() {
		return REMOTE_HOST_NAME;
	}
	
	/**
	 *  set the rootDirectory to store directory locations  
	 *  //TODO FileIO create directory if not exists
	 **/
	public static void setRootDirectory(String directory) { 
		DatabaseConnector.ROOT_DIRECTORY = directory; 
	}
	
	/**
	 * @return 
	 */
	public static String getRootDirectory() {
		
		Log.d("Class - ", "Connector");
		Log.d("Method - ", "getRootDirectory");
		Log.d("Root Directory : ", REMOTE_HOST_NAME.concat("/").concat(ROOT_DIRECTORY));
		
		return (new String("http://").concat(REMOTE_HOST_NAME).concat("/").concat(ROOT_DIRECTORY).concat("/"));
	}
	
	/**
	 *  set the sub-Directory to store directory locations  
	 *  //TODO FileIO create directory if not exists
	 **/
	public static void setSubDirectory(String sub) {
		DatabaseConnector.REMOTE_SUB = sub;
	}
	
	/**
	 * The Catalog Sub-Directory where files are stored in the host.
	 * @return the catalog directory
	 */
	public static String getSubDirectory()
	{
		return (new String("/").concat(DatabaseConnector.REMOTE_SUB.concat("/")));
	}
	
	public static String getWebApplicationDir() {
		return DatabaseConnector.getRootDirectory().concat("/").concat("application/");
	}
	
	public static String getWebUploader() {
		return getWebApplicationDir().concat("uploader.php");
	}
	
	public static String getIMService() {
		return getWebApplicationDir().concat("mysql.class.php");
	}
	
	public static String getIMServiceImpl()
	{
		return getWebApplicationDir().concat("index.php");
	}
	
	public static String getDBUsername()
	{
		return DB_USERNAME;
	}
	
	public static String getDBPassword()
	{
		return DB_PASSWORD;
	}
	
	
	// ------------------------------------------
	//					JDBC Methods
	// ------------------------------------------
	
	private static Statement ST = null;
	private static PreparedStatement PPS = null;
	private static Connection CONNECTION = null;
	
	/**
	 * @return Connection
	 * */
	public static Connection getConnection() throws SQLException {
		
		return ((Connection) DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD));
		
	}
	
	/**
	 * @return the statement for this connection
	 * @see 
	 * <a href="http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html">
	 * http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html</a>
	 */
	public static Statement getStatement() {
		
		try {
		
			Class.forName(DRIVER_NAME).newInstance();
			CONNECTION = (Connection) DriverManager.getConnection(getJDBC_URL(), DB_USERNAME, DB_PASSWORD);
			ST = (Statement) CONNECTION.createStatement();
			
		} catch (InstantiationException e) {
			
			Log.i("Class - ", "Connector");
			Log.e("Method - ", "getStatement()");
			Log.e("Block - ", "try/catch");
			Log.e("InstantiationException : ", e.toString());
			
		} catch (IllegalAccessException e) {
			
			Log.i("Class - ", "Connector");
			Log.e("Method - ", "getStatement()");
			Log.e("Block - ", "try/catch");
			Log.e("IllegalAccessException : ", e.toString());
			
		} catch (ClassNotFoundException e) {
			
			Log.i("Class - ", "Connector");
			Log.e("Method - ", "getStatement()");
			Log.e("Block - ", "try/catch");
			Log.e("ClassNotFoundException : ", e.toString());
			
		} catch (SQLException e) {
			
			Log.i("Class - ", "Connector");
			Log.e("Method - ", "getStatement()");
			Log.e("Block - ", "try/catch");
			Log.e("SQLException : ", e.toString());
		}
		
		return ST;
	}

	/**
	 * pre-prepare a SQL statement
	 * @param preparedStatement the SQL statement
	 * */
	public static void setPreparedStatement(String preparedStatement) {
		
		try {
			getConnection().setAutoCommit(false);
			DatabaseConnector.PPS = (PreparedStatement) getConnection().prepareStatement(preparedStatement, Statement.RETURN_GENERATED_KEYS);
			
		} catch (SQLException e) {
			Log.e("SQLException : ", e.toString());
		}
	}
	
	/**
	 * @return the statement for this connection
	 * @see 
	 * <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html">
	 * http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html</a>
	 */
	public static PreparedStatement getPreparedStatement() {
		return PPS;
	}

	public static String getJDBC_URL() {
		
		Log.i("Class - ", "Connector");
		Log.i("Method - ", "getJDBC_URL()");
		Log.i("jdbc url", JDBC_URL);
		
		
		return JDBC_URL;
	}
	
	
}
